﻿using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace MLogix
{
    public sealed class CryptLib
    {
        private static SymmetricAlgorithm _sa;
        static private string password = string.Empty;
        static private string message = string.Empty;

        public static string SimpleConfuscator(string inMessage)
        {
            string outString = string.Empty;
            foreach (char c in inMessage)
            {
                outString += (char)((int)c + 1);
            }
            return outString;
        }

        public static string SimpleDeConfuscator(string inMessage)
        {
            string outString = string.Empty;
            foreach (char c in inMessage)
            {
                outString += (char)((int)c - 1);
            }
            return outString;
        }
        /// <summary>
        /// Gets the plain text message
        /// </summary>
        public static string DecryptMessage
        {

            get
            {
                _sa = GenerateKey(password);
                return Decrypt(_sa, message);
            }
        }
        /// <summary>
        /// Encrypts a plain text
        /// </summary>
        public static string EncryptMessage
        {
            get
            {
                _sa = GenerateKey(password);
                return Encrypt(_sa, message);
            }
        }
        /// <summary>
        /// Message that has to be encrypted or decrypted
        /// </summary>
        public static string Message
        {
            set
            {
                message = value;
            }
        }
        /// <summary>
        /// Password which was used to encrypt the message
        /// </summary>
        public static string Password
        {
            set
            {
                password = value;
            }
        }

        private static SymmetricAlgorithm GenerateKey(string password)
        {
            RijndaelManaged sa = new RijndaelManaged();
            HashAlgorithm hashAlgo = new MD5CryptoServiceProvider();
            byte[] hash = hashAlgo.ComputeHash(Encoding.UTF8.GetBytes(password));
            sa.BlockSize = hash.Length * 8;
            sa.Key = hash;
            sa.IV = hash;
            return sa;
            //PASSWORD = Encrypt(sa);
            //Decrypt(sa);
        }
        private static string Encrypt(SymmetricAlgorithm sa, string message)
        {

            ICryptoTransform encryptor = sa.CreateEncryptor(sa.Key, sa.IV);
            ASCIIEncoding encoding = new ASCIIEncoding();
            byte[] inPass = encoding.GetBytes(message);
            MemoryStream outStream = new MemoryStream();
            CryptoStream outPass = new CryptoStream(outStream, encryptor, CryptoStreamMode.Write);
            outPass.Write(inPass, 0, inPass.Length);
            outPass.FlushFinalBlock();
            byte[] cipherTextBytes = outStream.ToArray();
            outStream.Close();
            outPass.Close();
            string cipherText = null;
            return cipherText = Convert.ToBase64String(cipherTextBytes);

        }
        private static string Decrypt(SymmetricAlgorithm sa, string cryptMessage)
        {
            ICryptoTransform decryptor = sa.CreateDecryptor(sa.Key, sa.IV);
            
            ASCIIEncoding encoding = new ASCIIEncoding();
           // byte[] inPass = encoding.GetBytes(PASSWORD);
            byte[] inPass = Convert.FromBase64String(cryptMessage);
            MemoryStream outStream = new MemoryStream(inPass);
            CryptoStream outPass = new CryptoStream(outStream, decryptor, CryptoStreamMode.Read);
            byte[] plainTextBytes = new byte[inPass.Length];
            int decryptedByteCount = outPass.Read(plainTextBytes, 0, plainTextBytes.Length);
            outStream.Close();
            outPass.Close();
            return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
        
        
        }
      
    }
}
